﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Collections;

namespace PMSSys
{
    public class paymentController
    {
        private DBconnentor dbConnect;
        public DBconnentor DbConnect
        {
            set { dbConnect = value; }
            get { return dbConnect; }
        }

        public bool add(Payment payment)
        {
            MySqlCommand query = new MySqlCommand(@"INSERT INTO accounting(p_id, account_date, account_serv, account_amount) 
            VALUES(@P_ID, @ACCOUNT_DATE, @ACCOUNT_SERV, @ACCOUNT_AMOUNT)", dbConnect.getConnection());

            query.Parameters.Add(new MySqlParameter("@P_ID", MySqlDbType.Int32)).Value = payment.PatientID;
            query.Parameters.Add(new MySqlParameter("@ACCOUNT_DATE", MySqlDbType.DateTime)).Value = payment.PayDate;
            query.Parameters.Add(new MySqlParameter("@ACCOUNT_SERV", MySqlDbType.String)).Value = payment.Services;
            query.Parameters.Add(new MySqlParameter("@ACCOUNT_AMOUNT", MySqlDbType.Float)).Value = payment.Amount;

            return dbConnect.nonQueryExecute(query);
        }
        public DataTable getDataTable(int p_id)
        {
            string str = "SELECT pay_id, account_date as `Date`, account_serv as `Account Service`, account_amount as `Amount` FROM accounting WHERE p_id = " + p_id.ToString();
            return dbConnect.view(str);
        }
        public bool update(Payment payment)
        {
            MySqlCommand query = new MySqlCommand(@"UPDATE accounting SET account_date = @ACCOUNT_DATE, account_serv = @ACCOUNT_SERV, 
            account_amount = @ACCOUNT_AMOUNT WHERE p_id = @P_ID AND pay_id = @PAY_ID", dbConnect.getConnection());

            query.Parameters.Add(new MySqlParameter("@P_ID", MySqlDbType.Int32)).Value = payment.PatientID;
            query.Parameters.Add(new MySqlParameter("@PAY_ID", MySqlDbType.Int32)).Value = payment.PaymentID;
            query.Parameters.Add(new MySqlParameter("@ACCOUNT_DATE", MySqlDbType.DateTime)).Value = payment.PayDate;
            query.Parameters.Add(new MySqlParameter("@ACCOUNT_SERV", MySqlDbType.String)).Value = payment.Services;
            query.Parameters.Add(new MySqlParameter("@ACCOUNT_AMOUNT", MySqlDbType.Float)).Value = payment.Amount;
            return dbConnect.nonQueryExecute(query);
        }
        public Payment objectFiller(Payment payObject)
        {
            string str = "SELECT * FROM accounting WHERE pay_id=" + payObject.PaymentID.ToString();
            DataTable temp = dbConnect.view(str);
            IEnumerator iter = temp.Rows[0].ItemArray.GetEnumerator();
            iter.MoveNext();
            iter.MoveNext();
            payObject.PatientID = (int)iter.Current;
            iter.MoveNext();
            payObject.PayDate = (DateTime)iter.Current;
            iter.MoveNext();
            payObject.Services = (String)iter.Current;
            iter.MoveNext();
            payObject.Amount = (float)iter.Current;

            return payObject;
        }
        public bool delete(string payID)
        {
            string str = "DELETE FROM accounting WHERE pay_id = " + payID;
            return dbConnect.delete(str);
        }

    }
}
